package com.asset.servlet;
/**
 *@ClassName RegisterServlet
 *@Author B21015118吴逸鑫
 *@Date 2022/6/9 08:10
 **/

/**
 * 注册
 * **/
import com.asset.bean.User;
import com.asset.dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String mail = request.getParameter("mail");
        String password = request.getParameter("password");
        String no = request.getParameter("no");
        String phone = request.getParameter("phone");
        String age = request.getParameter("age");
        String level = request.getParameter("level");

        User user = new User();
        user.setName(name);
        user.setPwd(password);
        user.setMail(mail);
        user.setNo(no);
        user.setPhone(phone);
        user.setLevel(level);
        user.setAge(age);

        UserDAO userDAO = new UserDAO();
        boolean flag = userDAO.register(user);
/**
 * 注册成功进入登录页面
 * 注册失败进入登录失败页面
 * **/
        if(flag){
            System.out.println("注册成功");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        } else{
            System.out.println("注册失败");
            request.getRequestDispatcher("register_error.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
